package com.ywjt.pinkelephant.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.pinkelephant.R;
import com.ywjt.pinkelephant.base.BaseBottomDialog;
import com.ywjt.pinkelephant.home.activity.ActivityPromptBroad;
import com.ywjt.pinkelephant.home.activity.ActivityPromptShot;
import com.ywjt.pinkelephant.home.adapter.ColorsAdapter;
import com.ywjt.pinkelephant.util.SPUtils;
import com.ywjt.pinkelephant.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;


public class PromptShotsetBottomDialog extends BaseBottomDialog {
    private Context mContext;
    private LinearLayout lltextColor,llBackColor,llMargin;
    private TextView tvTextColor,tvBackColor,tvAi;
    private ImageView ivDissmiss;
    private SeekBar sbTransparent,sbTextSize,sbSpeed;
    private TextView tvTransparent,tvTextSize,tvSpeed;
    private RecyclerView rcColor;
    private ColorsAdapter colorsAdapter;
    private SwitchButton sbAi;
    private List<String> colors;
    private int rate=0;

    private ClickListenerInterface clickListenerInterface;

    private int color=0;//0字体颜色  1 背景颜色

    public interface ClickListenerInterface {

    }

    public PromptShotsetBottomDialog(Context context) {
        this.mContext=context;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.bottom_prompt_boardset;
    }

    @Override
    public void bindView(View v) {
        rcColor=v.findViewById(R.id.rcColor);
        tvTextColor=v.findViewById(R.id.tvTextColor);
        tvBackColor=v.findViewById(R.id.tvBackColor);
        llMargin=v.findViewById(R.id.llMargin);
        sbTransparent=v.findViewById(R.id.sbTransparent);
        ivDissmiss=v.findViewById(R.id.ivDissmiss);
        sbSpeed=v.findViewById(R.id.sbSpeed);
        tvTextSize=v.findViewById(R.id.tvTextSize);
        tvAi=v.findViewById(R.id.tvAi);
        sbAi=v.findViewById(R.id.sbAi);
        tvSpeed=v.findViewById(R.id.tvSpeed);
        llBackColor=v.findViewById(R.id.llBackColor);
        tvTransparent=v.findViewById(R.id.tvTransparent);
        lltextColor=v.findViewById(R.id.lltextColor);
        sbTextSize=v.findViewById(R.id.sbTextSize);
        llMargin.setVisibility(View.GONE);
        ivDissmiss.setOnClickListener(new clickListener());
        lltextColor.setOnClickListener(new clickListener());
        llBackColor.setOnClickListener(new clickListener());
        tvAi.setOnClickListener(new clickListener());

        if (SPUtils.getSharedBooleanData(mContext,"IsChecked")){
            sbAi.setChecked(true);
        }else {
            sbAi.setChecked(false);
        }

        sbAi.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (isChecked){
                    SPUtils.setSharedBooleanData(mContext,"IsChecked",true);
                    ToastUtil toastUtil=new ToastUtil(mContext,R.layout.popu_success1,"已经设置Ai提词",1);
                    toastUtil.show();
                    dismiss();
                }else {
                    SPUtils.setSharedBooleanData(mContext,"IsChecked",false);
                    ToastUtil toastUtil=new ToastUtil(mContext,R.layout.popu_success1,"已经设置滚动提词",1);
                    toastUtil.show();
                    dismiss();
                }
            }
        });

        colors=new ArrayList<>();
        colors.add("ff0000");
        colors.add("ffec84");
        colors.add("000000");
        colors.add("0000ff");
        colors.add("00ff00");
        colorsAdapter=new ColorsAdapter(colors);
        rcColor.setAdapter(colorsAdapter);
        rcColor.setLayoutManager(new LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false));
        colorsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (color==0){
                    //字体颜色
                    tvTextColor.setBackgroundColor(Color.parseColor("#"+colors.get(position)));
                    sendBroadcast(2,"0",Color.parseColor("#"+colors.get(position)));
                }else {
                    tvBackColor.setBackgroundColor(Color.parseColor("#"+colors.get(position)));
                    sendBroadcast(2,"1",Color.parseColor("#"+colors.get(position)));
                }
            }
        });
        sbTextSize.setOnSeekBarChangeListener(seekBarChangeListener);
        sbTransparent.setOnSeekBarChangeListener(seekBarChangeListener);
        sbSpeed.setOnSeekBarChangeListener(seekBarChangeListener);

        if (SPUtils.getSharedIntData(mContext,"VideoTextColor")!=0){
           tvTextColor.setBackgroundColor(SPUtils.getSharedIntData(mContext,"VideoTextColor"));
        }

        if (SPUtils.getSharedIntData(mContext,"VideoBgColor")!=0){
            tvBackColor.setBackgroundColor(SPUtils.getSharedIntData(mContext,"VideoBgColor"));
        }

        if (SPUtils.getSharedFloatData(mContext,"VideoTextSize")!=0){
            sbTextSize.setProgress(SPUtils.getSharedFloatData(mContext,"VideoTextSize").intValue());
            tvTextSize.setText(SPUtils.getSharedFloatData(mContext,"BoardTextSize").intValue()+"%");
        }


        if (SPUtils.getSharedFloatData(mContext,"VideoTextTransparent")!=0){
            sbTransparent.setProgress(SPUtils.getSharedFloatData(mContext,"VideoTextTransparent").intValue());
            tvTransparent.setText(SPUtils.getSharedFloatData(mContext,"VideoTextTransparent")+"%");
        }

        if (SPUtils.getSharedIntData(mContext,"VideoTextSpeed")!=0){
            sbSpeed.setProgress(SPUtils.getSharedIntData(mContext,"VideoTextSpeed"));
            tvSpeed.setText(SPUtils.getSharedIntData(mContext,"VideoTextSpeed")+"%");
        }

    }

    public void setClicklistener(ClickListenerInterface clickListener) {
        clickListenerInterface = clickListener;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
              case  R.id.ivDissmiss:
                  dismiss();
                case R.id.lltextColor:
                    //字体颜色
                    color=0;
                    break;
                case R.id.llBackColor:
                    color=1;
                    break;
                case R.id.tvAi:
                    //ai
                    sendBroadcast(5,"",0);
                    break;
            }
        }
    }

    SeekBar.OnSeekBarChangeListener seekBarChangeListener=new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            switch (seekBar.getId()){
                case R.id.sbTransparent:
                    //透明度
                    rate=progress;
                    tvTransparent.setText(progress+"%");
                    sendBroadcast(1,rate+"",0);
                    break;
                case R.id.sbTextSize:
                    //字体大小
                    rate=progress;
                    tvTextSize.setText(progress+"%");
                    sendBroadcast(3,rate+"",0);
                    break;
                case R.id.sbSpeed:
                    rate=progress;
                    tvSpeed.setText(progress+"%");
                    sendBroadcast(5,rate+"",0);
                    break;
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) { }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    };

    //发送本地广播
    private void sendBroadcast(int action ,String value,int color) {
        Intent intent = new Intent(ActivityPromptShot.LOCAL_BROADCAST);
        intent.putExtra("action", action);
        intent.putExtra("value", value);
        intent.putExtra("color",color);
        ActivityPromptShot.localBroadcastManager.sendBroadcast(intent);
    }
}